package io.dy.service.impl;

import io.dy.service.IRedisService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : dmj
 * @date: 2024/7/1 10:25 上午
 * @description:
 */
@Service
@RequiredArgsConstructor
public class RedisServiceImpl implements IRedisService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    @Transactional
    public Long incr(String key) {
        return stringRedisTemplate.opsForValue().increment(key);
    }

    @Override
    @Transactional
    public void set() throws InterruptedException {
        //开启redis事物
        stringRedisTemplate.setEnableTransactionSupport(true);
        stringRedisTemplate.multi();
        stringRedisTemplate.opsForValue().set("test_01", "测试1");
        stringRedisTemplate.opsForValue().set("test_02", "测试2");
        stringRedisTemplate.exec();
    }
}
